package com.hongxun.design.utils.imageutil;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.utils.camerautil.CameraUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.io.File;

/**
 * Created by zerdoor_pc on 2016/5/12.
 */
public class UriUtil {

    public static Uri getUri(String path) {
        Uri u = null;

        if (path == null || path.length() == 0) return null;
        if (path.contains(MString.LOCALIMAGEHEAD)) {//资源
            u = Uri.parse(path);
            // MyLog.L(MString.SUCCESSED+"image_path= "+path);
        } else if (path.contains(Environment.getExternalStorageDirectory() + "/")) {//本地SD卡
            u = Uri.fromFile(new File(path));
            //  MyLog.L(MString.SUCCESSED+"image_path= "+path);
        } else if (path.contains(MyApp.File.getCachaPath())) {//缓存
            u = Uri.fromFile(new File(path));
            //   MyLog.L(MString.SUCCESSED+"image_path= "+path);
        } else if (!path.contains(HttpStringUtil.HeadSpeech)) {
            // MyLog.L(MString.SUCCESSED+"image_path= "+HttpStringUtil.HeadSpeech + "/" + path);
            u = Uri.parse(HttpStringUtil.HeadSpeech + "/" + path);
        } else {
            //其他
            u = Uri.parse(path);
        }

        return u;
    }

    public static String getString(String path) {
        String u = null;

        if (path == null || path.length() == 0) return null;
        if (path.indexOf(MString.LOCALIMAGEHEAD) != -1) {//资源
            u = path;
        } else if (path.indexOf(Environment.getExternalStorageDirectory() + "/") != -1) {//本地SD卡
            u = path;
        } else if (path.indexOf(HttpStringUtil.HeadSpeech) == -1 && path.indexOf(HttpStringUtil.HeadSpeech) == -1) {

            u = HttpStringUtil.HeadSpeech + "/" + path;
        } else {//其他
            u = path;
        }
        //MyLog.L(MString.SUCCESSED+"image_path= "+u);
        return u;
    }

    public static Uri getUrl(String path, int type) {
        if (path == null || path.length() == 0)
            return Uri.parse(MString.LOCALIMAGEHEAD + R.mipmap.zhanweizhenfang);

        if (path.contains(Environment.getExternalStorageDirectory() + "/")) {//本地SD卡
            return Uri.fromFile(new File(path));
        }
        String[] tar = path.split("/");
        String name = tar[tar.length - 1];
        File file = new File(MyApp.File.getCachaPhotoPath() + type + File.separator + name);
        MyLog.L(MString.SUCCESSED + MyApp.File.getCachaPhotoPath() + type + File.separator + name);

        if (file.exists()) {
            return Uri.fromFile(file);
        }
        File file2 = new File(MyApp.File.getCachaPhotoUuidPath() + type + File.separator + name);
        MyLog.L(MString.SUCCESSED + MyApp.File.getCachaPhotoUuidPath() + type + File.separator + name);

        if (file2.exists()) {
            return Uri.fromFile(file2);
        }
        MyLog.L(MString.SUCCESSED + "网络下载图片！:" + HttpStringUtil.HeadSpeech + "/" + path);

        loadUrl(HttpStringUtil.HeadSpeech + "/" + path, MyApp.File.getCachaPhotoPath() + type + File.separator + name);
        return Uri.parse(HttpStringUtil.HeadSpeech + "/" + path);
    }

    public static String getUrlpath(String path, int type) {
        if (path == null || path.length() == 0) return "";
        String[] tar = path.split("/");
        String name = tar[tar.length - 1];
        File file = new File(MyApp.File.getCachaPhotoPath() + type + File.separator + name);
        if (file.exists()) {
            return MyApp.File.getCachaPhotoPath() + type + File.separator + name;
        }
        File file2 = new File(MyApp.File.getCachaPhotoUuidPath() + type + File.separator + name);
        if (file2.exists()) {
            return MyApp.File.getCachaPhotoUuidPath() + type + File.separator + name;
        }

        MyLog.L(MString.SUCCESSED + MyApp.File.getCachaPhotoPath() + type + File.separator + name);
        MyLog.L(MString.SUCCESSED + MyApp.File.getCachaPhotoUuidPath() + type + File.separator + name);
        MyLog.L(MString.SUCCESSED + "网络下载图片！");

        loadUrl(HttpStringUtil.HeadSpeech + "/" + path, MyApp.File.getCachaPhotoPath() + type + File.separator + name);
        return HttpStringUtil.HeadSpeech + "/" + path;
    }

    private static void loadUrl(final String url, final String saveurl) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //下载服务器图片到本地
//                Bitmap bitmap = MyApp.getHttpBitmap(url);
                Bitmap bitmap = MyApp.byFresco(url);
                if (bitmap != null) {
                    CameraUtil.saveImage(bitmap, new File(saveurl));
                }
            }
        }).start();

    }

    public static Uri getImgFromService(String path) {
        if (path == null || path.length() == 0)
            return Uri.parse(MString.LOCALIMAGEHEAD + R.mipmap.zhanweizhenfang);
        if (path.contains(Environment.getExternalStorageDirectory() + "/")) {//本地SD卡
            return Uri.fromFile(new File(path));
        }
        String[] tar = path.split("/");
        String name = tar[tar.length - 1];
        File file = new File(MyApp.File.getCachaPhotoPath_Record() + "/" + name);
        if (file.exists()) {
            return Uri.fromFile(file);
        }
        String saveurl = MyApp.File.getCachaPhotoPath_Record() + "/" + name;
        loadUrl(HttpStringUtil.HeadSpeech + "/" + path, saveurl);
        return Uri.parse(MyApp.File.getCachaPhotoPath_Record() + "/" + name);
    }


}
